package com.fix.demo.day4;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

//@Component
public class MyTask2  implements SchedulingConfigurer{
	
	//默认的任务周期为 10秒
	private String cron = "0/10 * * * * ?";
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	public void setCron(String cron) {
		this.cron = cron;
	}

	@Override
	public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
		
		taskRegistrar.addTriggerTask(new Runnable() {
			
			int n=0;
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				System.out.println("执行任务2222--->" + sdf.format(new Date()));
				n++;
				if(n>3) {
					MyTask2.this.setCron("0/2 * * * * ?");
				}
			}
			
		}, new Trigger() {
			@Override
			public Date nextExecutionTime(TriggerContext triggerContext) {
				return new CronTrigger(cron).nextExecutionTime(triggerContext);
			}
		}); 
		
		 
	}
	

}

